package net.wofly.common.pay.alipay.service;

import com.egzosn.pay.ali.api.AliPayConfigStorage;
import com.egzosn.pay.ali.api.AliPayService;
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.common.util.sign.SignUtils;
import net.wofly.cashier.order.util.PayType;
import net.wofly.common.pay.AbstractPayProcessor;
import net.wofly.common.pay.PayContext;
import net.wofly.common.pay.alipay.config.AliPayConfig;
import net.wofly.common.pay.alipay.config.AlipayConfigDev;
import net.wofly.common.pay.util.ClientType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class AliPayProcessor extends AbstractPayProcessor {
    
    @Autowired
    AliPayConfig aliPayConfig;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private PayService service = null;
    
    private void initConfig(Integer clientType) {
        AliPayConfigStorage aliPayConfigStorage = new AliPayConfigStorage();
        aliPayConfigStorage.setPid("");
        aliPayConfigStorage.setAppId(aliPayConfig.getApp_id());
        aliPayConfigStorage.setKeyPublic(aliPayConfig.getAlipay_public_key());
        aliPayConfigStorage.setKeyPrivate(aliPayConfig.getMerchant_private_key());
        aliPayConfigStorage.setNotifyUrl(aliPayConfig.getNotify_url());
        aliPayConfigStorage.setReturnUrl(aliPayConfig.getReturn_url_pc());
        if (ClientType.移动端_H5.getValue().intValue() == clientType) {
            aliPayConfigStorage.setReturnUrl(aliPayConfig.getReturn_url_h5());
        }
        aliPayConfigStorage.setSignType(SignUtils.RSA2.name());
        aliPayConfigStorage.setSeller("");
        aliPayConfigStorage.setInputCharset("utf-8");
        //是否为测试账号，沙箱环境
        aliPayConfigStorage.setTest(aliPayConfig.getPayEnv().equals("dev"));
        if (aliPayConfigStorage.isTest()) {
            aliPayConfigStorage.setAppId(AlipayConfigDev.app_id);
            aliPayConfigStorage.setKeyPublic(AlipayConfigDev.alipay_public_key);
            aliPayConfigStorage.setKeyPrivate(AlipayConfigDev.merchant_private_key);
            aliPayConfigStorage.setNotifyUrl(AlipayConfigDev.notify_url);
            aliPayConfigStorage.setReturnUrl(AlipayConfigDev.return_url_pc);
            if (ClientType.移动端_H5.getValue().intValue() == clientType) {
                aliPayConfigStorage.setReturnUrl(AlipayConfigDev.return_url_pc);
            }
        }
        
        //请求连接池配置
        HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
        //最大连接数
        httpConfigStorage.setMaxTotal(20);
        //默认的每个路由的最大连接数
        httpConfigStorage.setDefaultMaxPerRoute(10);
        service = new AliPayService(aliPayConfigStorage, httpConfigStorage);
    }
    
    @Override
    public PayContext preCheck(PayContext payContext) {
        return null;
    }
    
    @Override
    public PayContext doPay(PayContext payContext) {
        logger.info(">>> 支付宝支付...");
        initConfig(payContext.getClientType());
        Map orderInfo = service.orderInfo(payContext.getPayOrder());
        payContext.setContent(service.buildRequest(orderInfo, MethodType.POST));
        return payContext;
    }
    
    @Override
    public Integer getPayChannel() {
        return PayType.支付宝.getValue();
    }
}
